package com.guyaning.deyson_core.core.ui.recycler;

import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.guyaning.deyson_core.core.R;
import com.guyaning.deyson_core.core.ui.banner.BannerCreator;
import com.guyaning.deyson_core.core.ui.image.GlideApp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/11.
 */

public class MultipleRecycleAdapter extends BaseMultiItemQuickAdapter<MultipleItemBean, MultipleViewHolder>
        implements BaseQuickAdapter.SpanSizeLookup, OnItemClickListener {

    //Banner是否是第一次加载，防止Item重新加载
    public boolean isFirst = false;
    private ConvenientBanner convenientBanner;

    public final RequestOptions OPTIONS = new RequestOptions()
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .dontAnimate();

    protected MultipleRecycleAdapter(List<MultipleItemBean> data) {
        super(data);
        init();
    }

    public static MultipleRecycleAdapter create(List<MultipleItemBean> data) {
        return new MultipleRecycleAdapter(data);
    }

    public static MultipleRecycleAdapter create(DataConverter converter) {
        return new MultipleRecycleAdapter(converter.convert());
    }

    @Override
    protected MultipleViewHolder createBaseViewHolder(View view) {
        return MultipleViewHolder.create(view);
    }

    @Override
    protected void convert(MultipleViewHolder helper, MultipleItemBean item) {

        final String text;
        final String imageUrl;
        final ArrayList<String> banners;

        switch (helper.getItemViewType()) {
            case ItemType.TEXT:
                text = item.getFiled(MultipleFields.TEXT);
                helper.setText(R.id.text_single, text);
                break;
            case ItemType.IMAGE:
                imageUrl = item.getFiled(MultipleFields.IMAGE_URL);

                GlideApp.with(mContext)
                        .load(imageUrl)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .centerInside()
                        .dontAnimate()
                        .into((ImageView) helper.getView(R.id.img_single));
                break;
            case ItemType.IMAGE_TEXT:

                imageUrl = item.getFiled(MultipleFields.IMAGE_URL);

                text = item.getFiled(MultipleFields.TEXT);

                GlideApp.with(mContext)
                        .load(imageUrl)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .centerInside()
                        .dontAnimate()
                        .into((ImageView) helper.getView(R.id.img_multiple));

                helper.setText(R.id.tv_multiple, text);

                break;

            case ItemType.BANNERS:

                banners = item.getFiled(MultipleFields.BANNERS);

                convenientBanner = helper.getView(R.id.banner_recycler_item);

                if (!isFirst) {

                    BannerCreator.setDefult(convenientBanner, banners, this);

                    isFirst = true;
                }
                break;
            default:
                break;
        }

    }

    private void init() {
        addItemType(ItemType.BANNERS, R.layout.item_multiple_banner);
        addItemType(ItemType.TEXT, R.layout.item_multiple_text);
        addItemType(ItemType.IMAGE, R.layout.item_multiple_image);
        addItemType(ItemType.IMAGE_TEXT, R.layout.item_multiple_image_text);

        //设置宽度的监听
        setSpanSizeLookup(this);
        openLoadAnimation();

        //是否多次执行动画
        isFirstOnly(false);
    }


    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {

        return getData().get(position).getFiled(MultipleFields.SPAN_SIZES);
    }

    @Override
    public void onItemClick(int position) {
        ToastUtils.showShort("点击了" + position);
    }
}
